import turtle as t
import random as ran

# init data
ball_y1 = ran.randint(-340, 340)
ball_y2 = ran.randint(-340, 340)
ball_y3 = ran.randint(-340, 340)
lst_balls_pos = [[530, ball_y1], [530, ball_y2], [530, ball_y3]]
star_x = ran.randint(-530, 530)
lst_star_pos = [star_x, 340]
# 1: left  0: right
ball_flag = 1
# overflag: 1: over 0: gaming
overflag = 0


# draw star
def draw_star(r, angle, color, x, y):
    t.up()
    t.goto(x, y)
    t.down()
    t.pencolor(color)
    t.fillcolor(color)
    t.begin_fill()
    for i in range(5):
        t.circle(r, angle)
        t.right(128)
    t.end_fill()


# draw star and balls
def draw(balls_pos, star_pos, r, angle, balls_color, star_color):
    t.clear()
    t.pencolor(balls_color)
    for i in range(3):
        t.up()
        t.goto(balls_pos[i][0], balls_pos[i][1])
        t.down()
        t.dot(20)
    draw_star(r, angle, star_color, star_pos[0], star_pos[1])


# move
def move():
    global overflag
    distance1 = float((abs(lst_balls_pos[0][0] - lst_star_pos[0]) ** 2 + abs(lst_balls_pos[0][1] - lst_star_pos[1]) ** 2)) ** 0.5
    distance2 = float((abs(lst_balls_pos[1][0] - lst_star_pos[0]) ** 2 + abs(lst_balls_pos[1][1] - lst_star_pos[1]) ** 2)) ** 0.5
    distance3 = float((abs(lst_balls_pos[2][0] - lst_star_pos[0]) ** 2 + abs(lst_balls_pos[2][1] - lst_star_pos[1]) ** 2)) ** 0.5
    if distance1 <= 18.0 or distance2 <= 18.0 or distance3 <= 18.0:
        overflag = 1
    if lst_star_pos[1] <= -355.0 or overflag == 1:
        t.pencolor('red')
        t.up()
        t.goto(-150, -50)
        t.down()
        t.write('Game Over!', font=('Arial', 40, 'bold'))
    else:
        if lst_balls_pos[0][0] <= -530.0:
            lst_balls_pos[0][0] = 530.0
            lst_balls_pos[1][0] = 530.0
            lst_balls_pos[2][0] = 530.0
        lst_balls_pos[0][0] -= 10
        lst_balls_pos[1][0] -= 10
        lst_balls_pos[2][0] -= 10
        lst_star_pos[1] -= 5
        draw(lst_balls_pos, lst_star_pos, 5, 200, 'black', 'green')
        t.ontimer(move, 20)


# click
def click(x, y):
    lst_star_pos[0] += ran.randint(-50, 50)
    lst_star_pos[1] += ran.randint(-80, 80)


t.setup(1080, 720)
t.ht()
t.tracer(False)
t.onscreenclick(click)
move()

t.done()
